package com.ljc.cjgl.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.ActivityAttachment;
import com.ljc.cjgl.entitiy.ClassActitvity;
import com.ljc.cjgl.entitiy.Stu;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.form.ActivityAttachmentForm;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.ActivityAttachmentService;
import com.ljc.cjgl.service.ClassActivityService;
import com.ljc.cjgl.service.ClassService;

@Controller
@RequestMapping(value="/activityAttachment")
public class ActivityAttachmentController {
	@Autowired
	private ActivityAttachmentService service;
	@Autowired
	private ClassActivityService activityService;
	@Autowired
	private ClassService classService;
	@Autowired
	private UserUtils userUtils;
	
	public Specification<ActivityAttachment> bulidSpec(ActivityAttachmentForm form){
		HashSet<Rule> rules=new HashSet<>();
		Integer classId = form.getClassId();
		if(classId!=null) {
			rules.add(Rule.property("activity.tbClass.id").equal(classId));
		}
		Integer activityId = form.getActivityId();
		if(activityId!=null) {
			rules.add(Rule.property("activity.id").equal(activityId));
		}
		Integer type = form.getType();
		if(type!=null) {
			rules.add(Rule.property("type").equal(type));
		}
		String search = form.getSearch();
		if(StringUtils.hasLength(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("title").contain(search));
			rules2.add(Rule.property("demo").contain(search));
			rules2.add(Rule.property("activity.tbClass.name").contain(search));
			rules2.add(Rule.property("activity.name").contain(search));
			rules2.add(Rule.property("creator").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping(value="/manage")
	public String manage(Integer activityId,ModelMap map) {
		Sort sort=Sort.by(new Order(Direction.DESC, "id"));
		List<TbClass> classes=classService.findAll(sort);
		List<ClassActitvity> actitvities=activityService.findAll();
		if(userUtils.hasRole("ROLE_STUDENT")) {
			Stu student=(Stu)userUtils.getUser();
			TbClass banji=student.getTbClass();
			classes.clear();
			classes.add(banji);
			actitvities=activityService.findByTbClassId(banji.getId());
		}
		if(activityId!=null) {
			actitvities.clear();
			ClassActitvity activity = activityService.findById(activityId);
			actitvities.add(activity);
			classes.clear();
			classes.add(activity.getTbClass());
		}
		map.put("activities",actitvities);
		map.put("activityId",activityId);
		map.put("classes", classes);
		return "activityAttachment/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,ActivityAttachmentForm form) {
		SysUser user=userUtils.getUser();
		if(userUtils.hasRole("ROLE_STUDENT")) {
			Stu student=(Stu) user;
			form.setClassId(student.getTbClass().getId());
		}
		Specification<ActivityAttachment> specification = bulidSpec(form);
		Page<ActivityAttachment> page=service.findAll(specification,pageable.bulidPageRequest());
		return TableResult.bulidPageResult(page);
	}
	
	
	@RequestMapping(value="/edit")
	public String edit(Integer id,Integer classId,Integer activityId,ModelMap map) {
		SysUser user=userUtils.getUser();
		List<ClassActitvity> actitvities=activityService.findAll();
		if(classId!=null) {
			actitvities=activityService.findByTbClassId(classId);
		}
		if(userUtils.hasRole("ROLE_STUDENT")) {
			Stu student=(Stu) user;
			actitvities=activityService.findByTbClassId(student.getTbClass().getId());
		}
		if(activityId!=null) {
			actitvities.clear();
			actitvities.add(activityService.findById(activityId));
		}
		ActivityAttachment model=new ActivityAttachment();
		if(id!=null) {
			model=service.findById(id);
		}
		map.put("activities",actitvities);
		map.put("model",model);
		return "activityAttachment/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(@RequestParam("file") MultipartFile file, ActivityAttachmentForm form,HttpServletRequest request) {
		if(form.getId()==null&&!StringUtils.hasText(file.getOriginalFilename())) {
			return TableResult.bulidResult(false, "新增附件时文件不能为空");
		}
		return service.save(form,request,file);
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<ActivityAttachment> attachments) {
		boolean flag=service.delete(attachments);
		if(flag) {
			return TableResult.bulidResult(true, "数据保存成功");
		}
		return TableResult.bulidResult(false, "数据保存失败");
	}
	
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id,String uid,HttpServletRequest request,HttpServletResponse response) {
		ActivityAttachment attachment=null;
		if(id!=null) {
			attachment=service.findById(id);
		}else {
			attachment=service.findByUid(uid);
		}
		FileUtil.download(request, response, attachment.getUid(), attachment.getFileName());
		return null;
	}
	
	@RequestMapping(value="/bjChange")
	@ResponseBody
	public Object bjChange(Integer bjId) {
		List<ClassActitvity> bjs=activityService.findByTbClassId(bjId);
		List<HashMap<String, Object>> result=new ArrayList<>();
		for (ClassActitvity ca : bjs) {
			HashMap<String, Object> m=new HashMap<>();
			m.put("value", ca.getId());
			m.put("text",ca.getName());
			result.add(m);
		}
		return result;
	}
	
}
